package com.freedom.base.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * Description: 自定义MyBatisPlus元数据处理器
 * <p>CreateTime: 2023-03-11 上午 2:07</p>
 * <p>Email: 2396598264@qq.com</p>
 *
 * @author Chb
 */

@Component
public class MPMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入操作，自动填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.hasSetter("bjtCreate")) {
            metaObject.setValue("bjtCreate", LocalDateTime.now());
        }
        if (metaObject.hasSetter("bjtModified")) {
            metaObject.setValue("bjtModified", LocalDateTime.now());
        }
    }

    /**
     * 更新操作，自动填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasSetter("bjtModified")) {
            metaObject.setValue("bjtModified", LocalDateTime.now());
        }
    }
}
